/*
 * @Author: zxc
 * @Date: 2021-01-23 16:00:56
 * @LastEditTime: 2021-01-23 18:17:56
 * @LastEditors: zxc
 * @Description: 柯里化
 */
import _ from "lodash"
const log = console.log
const checkAge = min => age => age >= min
// log(checkAge(18)(20))

/**
 * @Author: zxc
 * @Date: 2021-01-23 16:26:15
 * @func lodash->currying
 */
const getSum = (a, b, c) => {
  return a + b + c
}
let curried = _.curry(getSum)
// log(curried(1)(2, 3))

/**
 * @Author: zxc
 * @Date: 2021-01-23 16:39:26
 * @func: 案例
 */
// const matched = reg => str => str.match(reg)
const matched = _.curry(function (reg, str) {
  return str.match(reg)
})

// log(matched(/\s+/g, "hsell o3w 1ss"))
// log(matched(/\d+/g)("hsell o3w 1ss"))
const hasSpace = matched(/\s+/g)
const filter = _.curry(function (fn, array) {
  return array.filter(fn)
})
// log(filter(hasSpace)(["abc", "  s"]))

const findSpace = filter(hasSpace)
// log(findSpace(["asdfg", "a sdfgh"]))

/**
 * @Author: zxc
 * @Date: 2021-01-23 17:51:42
 * @func 实现curry
 */
const curry = fn => {
  const curriedFn = (...args) => {
    if (args.length < fn.length) {
      return (...brgs) => curriedFn(...args.concat(...brgs))
    }
    return fn(...args)
  }
  return curriedFn
}
function add(x, y, z) {
  return x + y + z
}
let curriedAdd = curry(add)
console.log(curriedAdd)
console.log(curriedAdd(1, 2)(2))
